package com.clstu.houserent.houseview;

import com.clstu.houserent.house.House;
import com.clstu.houserent.houseservice.HouseService;
import com.clstu.houserent.utility.Utility;

public class HouseView {
    private HouseService service=new HouseService(10);
    public void mainMenue(){
        boolean loop=true;
        String exit="";
        String key="";
        do{
            System.out.println("\n================房屋出租系统菜单==============");
            System.out.println("\t\t\t1 . 新 增 房 源");
            System.out.println("\t\t\t2 . 查 找 房 屋");
            System.out.println("\t\t\t3 . 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 . 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 . 房 屋 列 表");
            System.out.println("\t\t\t6 . 退       出");
            System.out.println("请输入需要的服务编号（1-6）：");
            key= Utility.myscan();
            switch (key){
                case "1":
                    System.out.println("===============新 增============");
                    if(service.add()){
                        System.out.println("添加成功");
                    }
                    else{
                        System.out.println("添加失败");
                    }
                    break;
                case "2":
                    System.out.println("=======================查找===================");
                    showSearch();
                    break;
                case "3":
                    System.out.println("======================删 除================");
                    if(!sureChoice()) break;
                    if(service.del()){
                        System.out.println("删除成功");
                    }
                    else{
                        System.out.println("删除失败");
                    }
                    break;
                case "4":
                    System.out.println("=====================修改=================");
                    if(!sureChoice()) break;
                    if(service.change()){
                        System.out.println("修改成功");
                    }
                    else{
                        System.out.println("修改失败");
                    }
                    break;
                case "5":
                    System.out.println("===============列  表============");
                    showList();
                    break;
                case "6":
                    System.out.println("==============退出==============");
                    if(!sureChoice()) break;
                    loop=false;
                    break;
                default:
                    System.out.println("输入有误，无效操作");
                    break;
                }

        }while(loop);

    }

    public boolean sureChoice(){
        String x="";
        System.out.println("确定吗？（y/n)");
        x=Utility.myscan();
        while (!x.equals("Y")&&!x.equals("y")&&!x.equals("n")&&!x.equals("N")){
            System.out.println("你的输入有误，请重新输入");
            x=Utility.myscan();
        }
        if(x.equals("n")||x.equals("N")){
            System.out.println("你已取消");
            return false;
        }
        return true;
    }

    public void showList(){
        House[] arr=service.list();
        System.out.println("房号 \t房主 \t电话 \t地址 \t价格 \t状态");
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==null) break;
            System.out.println(arr[i]);

        }
    }

    public void showSearch(){
        House h=service.serch();
        if(h==null) return;
        System.out.println("房号 \t房主 \t电话 \t地址 \t价格 \t状态");
        System.out.println(h);
    }


}
